/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Client_GUI.SavingType;

import Interface.IRemoteCustomer;
import Interface.IRemoteSavingType;
import Server.DTO.Customer_DTO;
import Server.DTO.SavingType_DTO;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 10520_000
 */
public class table_SavingType extends AbstractTableModel{
    //global variable
    Registry registry;
    IRemoteSavingType rsavingtype;
    DefaultTableModel model;
    private JTable table;
    
    public table_SavingType() throws RemoteException, NotBoundException{
        registry = LocateRegistry.getRegistry("127.0.0.1");
        rsavingtype = (IRemoteSavingType) registry.lookup("SavingType");
        LoadTable();
    }   
    
    //Funtions 
    public DefaultTableModel LoadTable() throws RemoteException, NotBoundException{ 
        //add header cho bang
        Vector Header=new Vector();
        Vector vtData = new Vector();
        Header.add("Mã Loại Tiết Kiệm");
        Header.add("Tên Loại Tiết Kiệm");
        Header.add("Kỳ Hạn");
        Header.add("Lãi Xuất");
        Header.add("Cho Phép Gửi");
        Header.add("Cho Phép Rút");
        
        //gan resource cho bang
        ArrayList arr = rsavingtype.FindAll();
        for (Iterator it = arr.iterator(); it.hasNext();) {
            SavingType_DTO info = (SavingType_DTO) it.next();
            Vector vtrow = new Vector();
            vtrow.add(info.getTypeID());
            vtrow.add(info.getName());
            vtrow.add(info.getTerm());
            vtrow.add(info.getInterestRate());
            vtrow.add(info.getAllowSave());
            vtrow.add(info.getAllowWithdraw());
            vtData.add(vtrow);
        }        
        //tra ve bang
        return new DefaultTableModel(vtData, Header);
    }
    //Funtions 
    public DefaultTableModel LoadTableSearch(SavingType_DTO info_savingtype) throws RemoteException, NotBoundException{ 
        //add header cho bang
        Vector Header=new Vector();
        Vector vtData = new Vector();
        Header.add("Mã Loại Tiết Kiệm");
        Header.add("Tên Loại Tiết Kiệm");
        Header.add("Kỳ Hạn");
        Header.add("Lãi Xuất");
        Header.add("Cho Phép Gửi");
        Header.add("Cho Phép Rút");
        
        //gan resource cho bang
        ArrayList arr = rsavingtype.GetSavingTypes(info_savingtype.getName());
        for (Iterator it = arr.iterator(); it.hasNext();) {
            SavingType_DTO info = (SavingType_DTO) it.next();
            Vector vtrow = new Vector();
            vtrow.add(info.getTypeID());
            vtrow.add(info.getName());
            vtrow.add(info.getTerm());
            vtrow.add(info.getInterestRate());
            vtrow.add(info.getAllowSave());
            vtrow.add(info.getAllowWithdraw());

            vtData.add(vtrow);
        }        
        //tra ve bang
        return new DefaultTableModel(vtData, Header);
    }

    @Override
    public int getRowCount() {
        return table.getRowCount();
    }

    @Override
    public int getColumnCount() {
        return table.getColumnCount();
    }

    @Override
    public Object getValueAt(int rowIndex, int columIndex) {
        return table.getValueAt(rowIndex, columIndex);
        
    }

    /**
     * @return the table
     */
    public JTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(JTable table) {
        this.table = table;
    }
    
}
